package cn.dengta.webapp.user.vo;

import java.util.Date;
import java.util.Map;

import cn.dengta.common.model.APIResponse;
import cn.dengta.common.model.ValueProxyImpl;
import cn.dengta.context.model.Country;
import cn.dengta.webapp.base.vo.AbstractVO;
import cn.dengta.webapp.user.entity.Moment;
import cn.dengta.webapp.user.entity.User;
import cn.dengta.webapp.user.entity.moment.MomentExt;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.*;
import me.codeplayer.util.StringUtil;

@Getter
@Setter
public class MomentVO extends AbstractVO<Moment> {

	private Long userId;
	/** 内容 */
	protected String content;
	/** 图片（多张图片以逗号隔开） */
	protected String images;
	/** 视频 */
	@JSONField(jsonDirect = true)
	protected String videos;
	/** 点赞数 */
	protected Integer digCount;
	/** 打赏金额 */
	private Integer donateAmount;
	protected Date updateTime;
	/** 是否可删除 */
	public Boolean canDelete;
	/** 是否可举报 */
	public Boolean canComplain;
	/** 是否可点赞 */
	public Boolean canDig;
	/** 是否可打赏 */
	public Boolean isDonated;
	/** 是否已点赞 */
	public Boolean isDug;
	@APIResponse.Field(name = "是否拉黑")
	public Boolean isBlock;
	@APIResponse.Field(name = "是否可取消拉黑")
	public Boolean canUnblock;
	/** 是否对外公开 */
	public Boolean open;
	/** 第一次搭讪 */
	@APIResponse.Field(name = "是否第一次搭讪")
	public Boolean isFirstChat = true;
	/** 是否关注 */
	@APIResponse.Field(name = "是否关注")
	public Boolean isFollowed;
	public Map<String, Object> user;
	public Object tag;
	public Map<String, Object> official;
	/** 是否可标记 */
	public Boolean canMark;
	/** 是否已标记 */
	public Boolean isMarked;
	/** 评论数 */
	private Integer commentCount;
	public Boolean online;

	String country;
	/** 扩展信息 */
	private MomentExt ext;

	public static MomentVO from(Moment source) {
		return copy(source, MomentVO::new);
	}

	public static MomentVO fromSimple(Moment source) {
		return copy(source, MomentVO::new, "digCount", "isFirstChat", "user", "updateTime", "donateAmount");
	}

	public static MomentVO popVo(@NonNull Moment source, @NonNull User sessionUser) {
		MomentVO vo = from(source);
		final boolean isSelf = sessionUser.eqId(source.getUserId());
		vo.canDelete = isSelf && source.canDelete();
		if (!isSelf) {
			vo.canComplain = source.canShow();
		}
		vo.canDig = source.canDig();
		vo.open = source.canShow();
		return vo;
	}

	public void putAvatarFrame(String frame) {
		if (this.user != null && StringUtil.notEmpty(frame)) {
			this.user.put("avatarFrame", frame);
		}
	}

	public String getAddTime_() {
		return super.getAddTime_();
	}

	public String getCountry_(){
		if (StringUtil.isEmpty(this.country)) {
			return null;
		}
		return ValueProxyImpl.label(Country.of(this.country));
	}

	@Override
	public Date getAddTime() {
		return super.getAddTime();
	}

}
